package com.shijie.controller.app;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.component.redis.RedisClient;
import com.shijie.component.redis.RedisKey;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.Activitys;
import com.shijie.domain.entity.Products;
import com.shijie.domain.entity.Skus;
import com.shijie.domain.entity.UserAttentions;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.domain.vo.CartVo;
import com.shijie.domain.vo.SkuVo;
import com.shijie.mapper.SkusMapper;
import com.shijie.service.ActivitysService;
import com.shijie.service.ProductsService;
import com.shijie.service.UserAttentionsService;

/*
 *  产品等相关接口
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/products")
public class AppProductsController {

    @Resource
    private ProductsService productsService;

    @Resource
    private AppUserService appUserService;

    @Resource
    private UserAttentionsService userAttentionsService;

    @Resource
    private ActivitysService activitysService;

    @Autowired
    private RedisClient redisClient;
    
    @Autowired
    private SkusMapper skuMapper;

    @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "20") Integer pageSize,
                       @RequestParam(required = false) Integer brandId, @RequestParam(required = false) Integer activityId,
                       @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String q) {
        PageHelper.startPage(page, pageSize);
        List<Products> list = productsService.selectProducts(brandId, activityId, categoryId, q);
        PageInfo<Products> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
    }
    
    @GetMapping("/ids")
    public Result getProductByIds(@RequestParam String ids) {
        List<Products> list = productsService.selectByIds(ids);
        return ResultGenerator.success(list);
    }
    
    @PostMapping("/skus")
    public Result filterCartSkus(@RequestBody CartVo cartVo) {
    	List<Products> products = new ArrayList<>();
    	cartVo.getSkus().forEach(c->{
    		Products product = productsService.selectById(c.getId());
    		if(product!=null){
    			Skus sku = skuMapper.selectByPrimaryKey(c.getSkuId());
    			if(sku!=null){
            		SkuVo  skuVo = new SkuVo(sku);
            		skuVo.setBuyNumber(c.getBuyNumber());
            		skuVo.setSkuId(c.getSkuId());
            		product.setSku(skuVo);
            		products.add(product);
    			}
    		}
    	});
        return ResultGenerator.success(products);
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        Products products = productsService.selectById(id);
        // 判断是否关注商品
        UserAttentions attentions = userAttentionsService.isAttentions(appUserService.getCurrentUser().getId(),
                EnumInterface.EntityType.PRODUCT.ordinal(), id, EnumInterface.Status.VALID.ordinal());
        products.setIsAttentions(attentions == null ? Boolean.FALSE : Boolean.TRUE);
        // 活动信息展示 默认显示最新活动
        List<Activitys> activitys = activitysService.listByProductId(products.getId());
        if (activitys != null && activitys.size() > 0) {
            products.setActivitys(activitys.get(0));
        }

        // 增加此商品的分类的点击数,商品pv ，存入redis
        redisClient.incrementScoreForZet(String.format(RedisKey.APP_USER_CATEGORY, appUserService.getCurrentUser().getId()),
                products.getCategoryId(), 1D);
        redisClient.incrementScoreForZet(RedisKey.APP_PRODUCT_PV, products.getId(), 1D);

        return ResultGenerator.success(products);
    }
}

